/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import database.SuratTabel;
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import model.Surat;

/**
 *
 * @author Adell
 */
public class PanelLihatTabelSurat extends JPanel {

    private JTable tabelSurat;
    private JLabel labelsurat;
    private ArrayList<Surat> data;

    public PanelLihatTabelSurat(ArrayList<Surat> data) {
        this.data = data;

        setLayout(new BorderLayout());
        labelsurat = new JLabel("Label Surat");
        labelsurat.setHorizontalAlignment(JLabel.CENTER);
        add(labelsurat, BorderLayout.NORTH);

        tabelSurat = new JTable();
        tabelSurat.setModel(new SuratTabel(data));
        add(tabelSurat, BorderLayout.CENTER);
        setVisible(true);

        tabelSurat.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = tabelSurat.rowAtPoint(e.getPoint());
                String pen = tabelSurat.getValueAt(row, 3).toString();
                String pen1 = String.valueOf(pen);
                Surat sur = new Surat();
                sur.setPenerimaSurat(pen1);
                Connection con = model.Conection.open("jdbc:oracle:thin:@localhost:1521:XE", "hr", "sisil");
                Statement stat = null;
                try {
                    stat = con.createStatement();
                } catch (SQLException ex) {
                    Logger.getLogger(PanelLihatTabelSurat.class.getName()).log(Level.SEVERE, null, ex);
                }
                  String update = "update surat set KETERANGAN = 'Diterima' where PENERIMA = '" + sur.getPenerimaSurat() + "'";
                try {
                    stat.executeQuery(update);
                    JOptionPane.showMessageDialog(null, "Surat telah terbaca");
                } catch (SQLException ex) {
                    Logger.getLogger(PanelLihatTabelSurat.class.getName()).log(Level.SEVERE, null, ex);
                }
                  
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });

    }

    public ArrayList<Surat> getData() {
        return data;
    }

    public void setData(ArrayList<Surat> data) {
        this.data = data;
    }
}
